import 'package:dio/dio.dart';
import 'package:flutter/material.dart';
import 'package:jdflutter/flutter_jdshop/config/Config.dart';
import 'package:jdflutter/flutter_jdshop/model/ProductContentModel.dart';
import 'package:jdflutter/flutter_jdshop/model/ProductModel.dart';
import 'package:jdflutter/flutter_jdshop/pages/ProductContent/ProductContentFirst.dart';
import 'package:jdflutter/flutter_jdshop/pages/ProductContent/ProductContentSecond.dart';
import 'package:jdflutter/flutter_jdshop/pages/ProductContent/ProductContentThird.dart';
import 'package:jdflutter/flutter_jdshop/services/ScreenAdaper.dart';
import 'package:jdflutter/flutter_jdshop/widget/JbButton.dart';
import 'package:jdflutter/flutter_jdshop/widget/LoadingWidget.dart';

/// 广播
import '../services/EventBus.dart';

class ProductContentPage extends StatefulWidget {
  final Map arguments;
  const ProductContentPage({Key? key,required this.arguments}) : super(key: key);

  @override
  _ProductContentPageState createState() => _ProductContentPageState();
}

class _ProductContentPageState extends State<ProductContentPage> {
  List _productContentList=[];

  @override
  void initState(){
    super.initState();
    this._getContentData();

  }

  // 商品详情接口
  _getContentData() async{
    print("商品详情接口id"+ widget.arguments['id']);
   // var api = '${Config.domain}api/pcontent?id=${widget.arguments['id']}';
    var api ='${Config.domain}api/pcontent?id=${widget.arguments['id']}';
    print("商品详情接口");
    var result = await Dio().get(api);
    var productContent = new ProductContentModel.fromJson(result.data);

    setState(() {
      // 将返回追加list数组
      this._productContentList.add(productContent.result);
    });
  }
  @override
  Widget build(BuildContext context) {
    return DefaultTabController(
        length: 3,
        child: Scaffold(
          appBar: AppBar(
            title: Row(
              mainAxisAlignment: MainAxisAlignment.center,
              children: <Widget>[
                Container(
                  width: ScreenAdaper.width(400),
                  child: TabBar(
                    indicatorColor: Colors.red,
                    indicatorSize: TabBarIndicatorSize.label,
                    tabs: [
                      Tab(
                        child: Text('商品'),
                      ),
                      Tab(
                        child: Text('详情'),
                      ),
                      Tab(
                        child: Text('评价'),
                      ),
                    ],
                  ),
                  
                )
              ],
            ),
            actions: [
              IconButton(
                  icon: Icon(Icons.more_horiz),
                  onPressed: (){
                    showMenu(
                        context: context,
                        position: RelativeRect.fromLTRB(ScreenAdaper.width(600), 76, 10, 0),
                        items: [
                          PopupMenuItem(
                              child: Row(
                                children: [
                                  Icon(Icons.home),
                                  Text("首页")
                                ],
                              )
                          ),
                          PopupMenuItem(
                              child: Row(
                                children: [
                                  Icon(Icons.search),
                                  Text("搜索")
                                ],
                              )
                          )
                        ]
                    );
                  },
                  )
            ],
          ),
          // 做了 优化
          body: this._productContentList.length>0?Stack(
            children: [
              TabBarView(
                  children: [
                    // 组件传递数据
                    ProductContentFirst(this._productContentList),
                    ProductContentSecond(this._productContentList),
                    ProductContentThird(),
                  ]
              ),
              Positioned(
                  width: ScreenAdaper.width(750),
                  height: ScreenAdaper.width(88),
                  bottom: 0,
                  child:Container(
                    decoration:BoxDecoration(
                      border: Border(
                        top: BorderSide(
                          color: Colors.black26,
                          width: 1
                        )
                      ),
                      color: Colors.white
                    ),
                    child: Row(
                      children: [
                        Container(
                          padding: EdgeInsets.only(top: ScreenAdaper.height(8)),
                          width: 100,
                          height: ScreenAdaper.height(80),
                          child: Column(
                            children: [
                              Icon(Icons.shopping_cart,size: ScreenAdaper.size(38)),
                              Text("购物车",style: TextStyle(fontSize: ScreenAdaper.size(24)))
                            ],
                          ),
                        ),
                        Expanded(
                            flex: 1,
                            child: JdButton(
                              color: Color.fromRGBO(253, 1, 0, 0.9),
                              text: "加入购物车",
                              /// 购物车之 event_bus事件广播   调用ProductContentFirst 底部
                              ///  从一个事件去触发另一个事件
                              cb: (){
                                if(this._productContentList[0].attr.length>0){
                                  eventBus.fire(new ProductContentEvent('加入购物车'));  ///进行广播

                                }else{
                                  print("加入购物车");
                                }


                                //Fluttertoast.showToast(msg: "加入购物车");
                              },
                            )
                        ),
                        Expanded(
                            flex: 1,
                            child: JdButton(
                              color: Color.fromRGBO(253, 1, 0, 0.9),
                              text: "立即购买",
                              cb: (){
                                //Fluttertoast.showToast(msg: "立即购买");
                                if(this._productContentList[0].attr.length>0){
                                  eventBus.fire(new ProductContentEvent('立即购买'));  ///进行广播

                                }else{
                                  print("立即购买");
                                }

                              },
                            )
                        )
                      ],
                    ),
                  ) 
              )
            ],
          ):LoadingWidget(),
        ));
  }
}
